package org.goj.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.goj.common.constant.RedisConstant;
import org.goj.common.entity.user.Resource;
import org.goj.common.entity.user.RoleResource;
import org.goj.user.mapper.ResourceMapper;
import org.goj.user.service.ResourceService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author CK
 * @since 2020-12-16
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {

    @javax.annotation.Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @javax.annotation.Resource
    private ResourceMapper resourceMapper;

    @Override
    public Boolean initResourceRolesMap() {
        try {
            QueryWrapper<Resource> resourceQueryWrapper = new QueryWrapper<>();
            resourceQueryWrapper.select("resource_url");
            Set<Resource> resources = new HashSet<>(super.list(resourceQueryWrapper));
            Map<String, List<String>> resourceRolesMap = resources.stream()
                    .collect(Collectors.toMap(Resource::getResourceUrl, t -> {
                        List<RoleResource> list = resourceMapper.getRoleIdByResourceUrl(t.getResourceUrl());
                        return list.stream().map(a -> a.getRoleId().toString()).collect(Collectors.toList());
                    }));
            redisTemplate.delete(RedisConstant.RESOURCE_ROLES_MAP);
            redisTemplate.opsForHash().putAll(RedisConstant.RESOURCE_ROLES_MAP, resourceRolesMap);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Map<String, Object>> listResource() {
        Map<Integer, String> idMap = Map.of(1, "management", 2, "practice", 3, "user");
        List<Map<String, Object>> list = new ArrayList<>();
        List<Resource> list1 = super.list();
        for (int i = 1; i <= 3; i++) {
            Map<String, Object> listResource = new HashMap<>(1);
            listResource.put("id", i * 1000);
            listResource.put("label", i == 1 ? "管理服务" : i == 2 ? "练习服务" : "用户服务");
            int finalI = i;
            listResource.put("children",
                    list1.stream().filter(t -> idMap.get(finalI).equals(t.getResourceUrl().split("/")[1]))
                            .map(t -> {
                                Map<String, Object> map = new HashMap<>(1);
                                map.put("id", t.getResourceId());
                                map.put("label", t.getResourceDesc());
                                return map;
                            })
                            .collect(Collectors.toList()));
            list.add(listResource);
        }
        return list;
    }
}
